<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2018/1/13
 * Time: 16:14
 */

namespace App\Http\Logics\MiniProgramAdmin;


use App\PartyCategory;
use App\Http\Logics\BaseLogic;
class PartyCategoryLogic extends BaseLogic
{
    /**
     * 创建活动分类
     * @param $data 分类信息
     * @return mixed
     */
    public static function createPartyCategory($data)
    {
        try{
            $response = self::getPartnerIdByLoginSession($data['loginSession']);
            if ($response->code){
                return self::response($response->code);
            }
            $partner_id = $response->data;
            $party_category = new PartyCategory();
            $party_category->partner_id = $partner_id;
            $party_category->name = $data['name'];
            $party_category->cover = $data['cover'];
            if (!$party_category->save()){
                return self::response(self::OBJECT_SAVE_FAIL);
            }
            return self::response(self::SUCCESS);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    /**
     * 获取活动分类列表
     * @param $data 筛选条件
     * @return mixed
     */
    public static function getPartyCategoryList($data)
    {
        try{
            // 通过key获取到合作者信息
            $response = self::getPartnerIdByKey($data['key']);
            if ($response->code){
                return self::response(self::CONTENT_IS_EMPTY);
            }
            $partner_id = $response->data;

            $party_category = PartyCategory::where('partner_id', $partner_id)->orderBy('id', 'desc')->get();
            if (empty($party_category)){
                return self::response(self::CONTENT_IS_EMPTY);
            }
            return self::response(self::SUCCESS, $party_category);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    /**
     * 获取活动分类详情
     * @param $data 筛选条件
     * @return mixed
     */
    public static function getPartyCategoryDetail($data)
    {
        try{
            $party_category = PartyCategory::find($data['party_category_id']);
            if (empty($party_category)){
                return self::response(self::CONTENT_IS_EMPTY);
            }
            return self::response(self::SUCCESS, $party_category);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    /**
     * 更新活动分类
     * @param $data 分类信息
     * @return mixed
     */
    public static function updatePartyCategory($data)
    {
        try{
            $party_category = PartyCategory::find($data['party_category_id']);
            if (empty($party_category)){
                return self::response(self::CONTENT_IS_EMPTY);
            }
            $party_category->name = $data['name'];
            $party_category->cover = $data['cover'];
            if (!$party_category->save()){
                return self::response(self::OBJECT_SAVE_FAIL);
            }
            return self::response(self::SUCCESS);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    public static function deletePartyCategory($data)
    {
        try{
            $party_category = PartyCategory::find($data['party_category_id']);
            if (!$party_category->delete()){
                return self::response(self::OBJECT_DELETE_FAILD);
            }
            return self::response(self::SUCCESS);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }
}